package com.joven.shelltest.controller;

import com.joven.shelltest.service.ShellService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务器上脚本执行
 * @Author wzj
 * @Date 2022-09-15 16:28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/joven")
public class ShellController {

    private final ShellService shellService;

    @GetMapping("/test")
    public void testShell(String path) {
        //执行的脚本，一个字符串就是一行。
        String[] strs = {"cp aaa.sh aaa2.sh"};
        try {
            shellService.createShell(path, strs);
            shellService.runShell(path);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * localhost:8080/joven/call?path=/home/shellTest/test.sh
     * @param path 脚本绝对路径
     */
    @GetMapping("/call")
    public void testShell2(String path) {
        String args = "";
//        String workspace = "/home/shellTest";
        try {
            //shellService.callShell(path, args, SubUtils.subPath(path));

            //localhost:8080/joven/call?path=/home/shellTest/countirs.log
            shellService.callShell(path, args, null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @GetMapping("/test11")
    public void testShell3() {
        String[] path = new String[]{"node3countirs" , "node4countirs" , "node5countirs" , "node9countirs", "node10countirs" };
        shellService.executeCountFlux(path);
    }

    @GetMapping("/hello")
    public String hello(String path) {
        return "test hello " + path;
    }
}
